<?php

/**
 * Created by PhpStorm.
 * User: fanfz_000
 * Date: 2015/4/4 0004
 * Time: 16:50
 * 作用 ：文章控制器
 */
class Word extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    //word详细信息
    function word_list($Id = 0)
    {
//        //缓存60分钟更新一次
        $this->output->cache(60);
//
        //模型操作，图片轮播图片列表
        $this->load->model('Backgroudurl_model', 'image', TRUE);
        $data['image_list'] = $this->image->show_image_list();

        //模型操作,标签云
        $this->load->model('Label_model', 'label', TRUE);
        $data['label_list'] = $this->label->show_label_list();

        //模型操作,文章
        if ($Id == 0) {
            $Id_array = $this->db->select_min('Id')->from('word')->get()->row_array();
            $Id = $Id_array['Id'];
        }
        $this->load->model('Word_model', 'word', TRUE);
        $data['word_info'] = $this->word->show_word_Info($Id);

        //模型操作,文章列表
        $data['word_list'] = $this->word->show_word_list(0, 30);

        //模型操作,友情链接
        $this->load->model('Friendhref_model', 'friend_href', TRUE);
        $data['friend_list'] = $this->friend_href->show_friend_href_list();

        //页面加载
        $this->load->view('header', $data);
        $this->load->view('word_header');
        $this->load->view('word_list');
        $this->load->view('word_aside');
        $this->load->view('footer');
    }

    /*显示搜索文章*/
    function search_word_list()
    {
        $search = $this->input->post('search_word');
        //缓存60分钟更新一次
        $this->output->cache(60);
//
        //模型操作，图片轮播图片列表
        $this->load->model('Backgroudurl_model', 'image', TRUE);
        $data['image_list'] = $this->image->show_image_list();

        //模型操作,标签云
        $this->load->model('Label_model', 'label', TRUE);
        $data['label_list'] = $this->label->show_label_list();

        //模型操作,文章
        $this->load->model('Word_model', 'word', TRUE);
        $Id = $this->db->select('Id')->from('word')->like('title', $search)->get()->row_array();
        if (empty($Id)) {
            $data['word_info']['word_info']['Id'] = '';
            $data['word_info']['word_info']['title'] = "没找到数据";
            $data['word_info']['word_info']['text'] = "<a href='".constant("APP")."'>返回首页</button>";
            $data['word_info']['word_info']['datetime'] = '';
            $data['word_info']['word_info']['label_id'] = '';
            $data['word_info']['word_info']['label_name'] = '';
            $data['word_info']['last'] = array(
                'Id' => '',
                'title' => '已经是第一页',
                'class' => 'previous disabled'
            );
            $data['word_info']['next'] = array(
                'Id' => '',
                'title' => '已经是最后一页',
                'class' => 'next disabled'
            );
        } else {

            $data['word_info'] = $this->word->show_word_Info($Id['Id']);
        }
        //模型操作,文章列表
        $data['word_list'] = $this->word->show_word_list(0, 30);

        //模型操作,友情链接
        $this->load->model('Friendhref_model', 'friend_href', TRUE);
        $data['friend_list'] = $this->friend_href->show_friend_href_list();

        //页面加载
        $this->load->view('header', $data);
        $this->load->view('word_header');
        $this->load->view('word_list');
        $this->load->view('word_aside');
        $this->load->view('footer');
    }
    function word_info_list()
    {
        $page = $this->input->post("page");
        $limit = $this->input->post("limit");
        //模型操作,显示文章列表
        $this->load->model('Word_model', 'word', TRUE);
        $data = $this->word->show_word_list($page, $limit);
        echo json_encode($data);
    }

    /*搜索文章列表 2015-4-29 */
    function search_word()
    {
        $search = $this->input->post("search");
        $this->load->model('Word_model', 'word', TRUE);
        $data = $this->word->search_word_list($search);
        echo json_encode($data);
    }

    function show_search_word($Id)
    {
        //缓存60分钟更新一次
        $this->output->cache(60);
//
        //模型操作，图片轮播图片列表
        $this->load->model('Backgroudurl_model', 'image', TRUE);
        $data['image_list'] = $this->image->show_image_list();

        //模型操作,标签云
        $this->load->model('Label_model', 'label', TRUE);
        $data['label_list'] = $this->label->show_label_list();

        //模型操作,文章
        $this->load->model('Word_model', 'word', TRUE);
        $data['word_info'] = $this->word->show_search_word($Id);

        //模型操作,文章列表
        $data['word_list'] = $this->word->show_word_list(0, 30);

        //模型操作,友情链接
        $this->load->model('Friendhref_model', 'friend_href', TRUE);
        $data['friend_list'] = $this->friend_href->show_friend_href_list();

        //页面加载
        $this->load->view('header', $data);
        $this->load->view('word_header');
        $this->load->view('word_list');
        $this->load->view('word_aside');
        $this->load->view('footer');
    }

    /*根据id更新文章 2015-5-4*/
    function update_word_by_id()
    {
        $word_id = $this->input->post("word_id");
        $word_title = $this->input->post("word_title");
        $label_id = $this->input->post("label_id");
        $word_text = $this->input->post("word_text");
        $this->load->model('Word_model', 'word', TRUE);
        $data = $this->word->update_word($word_id, $word_title, $label_id, $word_text);
        echo json_encode($data);
    }

    /*根据id删除文章 2015-5-4*/
    function delete_word()
    {
        $word_id = $this->input->post("Id");
        $this->load->model('Word_model', 'word', TRUE);
        $data = $this->word->delete_word($word_id);
        echo json_encode($data);
    }

    /*新建文章 2015-5-4*/
    function create_word()
    {
        $word_title = $this->input->post("word_title");
        $label_id = $this->input->post("label_id");
        $word_text = $this->input->post("word_text");
        $this->load->model('Word_model', 'word', TRUE);
        $data = $this->word->insert_word($word_title, $label_id, $word_text);
        echo json_encode($data);
    }

    //文章详细
    function word_by_id_info()
    {
        $Id = $this->input->post('id');
        $this->load->model('Word_model', 'word', TRUE);
        $word = $this->word->show_word_info($Id);
        echo json_encode($word);
    }

}